/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.comm.listener.impl;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.kopsox.comm.message.impl.ValueMessageImpl;
import org.kopsox.data.Value;
import org.kopsox.data.impl.Polymorph;
import org.kopsox.general.type.ValueTypeEnum;

/**
 * This AbstractClass makes it possible, that a child of this class, can be a
 * Observer of the KopsoxFramework<br>
 * and a PropertyChangeListener. The child of this class has just to implement
 * the notification-Method
 * 
 * @author Konrad Renner
 * 
 */
public abstract class AbstractPropertyChangeObserver extends AbstractObserver implements
		PropertyChangeListener{

	private static final long serialVersionUID = 5347731578023352086L;

	protected static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
	 * PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(final PropertyChangeEvent event) {
		Object newValue = event.getNewValue();
		Object oldValue = event.getOldValue();
		Object source = event.getSource();
		String name = event.getPropertyName();
		
		//Convert to a format which can be handled with the framework
		String newValueStr;
		if(newValue == null) {
			newValueStr = null;
		}else if(newValue instanceof java.sql.Timestamp || newValue instanceof java.sql.Time) {
			newValueStr = newValue.toString();
		}else if(newValue instanceof java.util.Date) {
			newValueStr = DATE_FORMAT.format((java.util.Date)newValue);
		}else {
			newValueStr = newValue.toString();
		}
		
		String oldValueStr;
		if(oldValue == null) {
			oldValueStr = null;
		}else if(oldValue instanceof java.sql.Timestamp || oldValue instanceof java.sql.Time) {
			oldValueStr = oldValue.toString();
		}else if(oldValue instanceof java.util.Date) {
			oldValueStr = DATE_FORMAT.format((java.util.Date)oldValue);
		}else {
			oldValueStr = oldValue.toString();
		}
		
		// Message creation
		Value<String> value = new Polymorph<String>(newValueStr, name);
		ValueMessageImpl message = ValueMessageImpl.createValueMessage(value);
		message.setMessageSender(source);
		message.setOldValue(oldValueStr);
		message.setTypeFromOldValue(ValueTypeEnum.getTypeFromValue(oldValue));

		// Sending the message
		notification(message);
	}

}
